using System.Diagnostics;
using System.Reflection;
using System.Windows.Input;
using Wpf.Ui.Abstractions.Controls;

namespace FileTransferDesktop.ViewModels.Pages;

public partial class AboutViewModel : ObservableObject, INavigationAware
{
    private bool _isInitialized = false;

    [ObservableProperty]
    private string _appVersion = string.Empty;

    [ObservableProperty]
    private string _appDescription =
        "File Transfer Desktop 是一款简单高效的文件传输工具，支持设备间快速传输各类文件。";

    [ObservableProperty]
    private string _developer = "开发者团队";

    [ObservableProperty]
    private string _copyright = $"Copyright © {DateTime.Now.Year}";

    [ObservableProperty]
#if DEBUG
    private string _content = "（此处内容会在 Release 构建中显示）";
#else
#pragma warning disable CS0618 // Type or member is obsolete
    private string _content = ThisAssembly.Resources.README.Text;
#pragma warning restore CS0618 // Type or member is obsolete
#endif

    // 添加打开官网命令
    public ICommand OpenWebsiteCommand { get; }

    public AboutViewModel()
    {
        OpenWebsiteCommand = new RelayCommand(OpenWebsite);
    }

    private void OpenWebsite()
    {
        Process.Start(new ProcessStartInfo
        {
            FileName = "https://gitcode.com/file_transfer/file_transfer_desktop",
            UseShellExecute = true
        });
    }

    public Task OnNavigatedToAsync()
    {
        if (!_isInitialized)
            InitializeViewModel();

        return Task.CompletedTask;
    }

    public Task OnNavigatedFromAsync() => Task.CompletedTask;

    private void InitializeViewModel()
    {
        AppVersion = $"版本: {GetAssemblyVersion()}";
        _isInitialized = true;
    }

    private static string GetAssemblyVersion()
    {
        return Assembly.GetExecutingAssembly().GetName().Version?.ToString() ?? string.Empty;
    }
}
